package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.component.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author dyq
 * @description
 * @data 27/12/2021
 */
@RequestMapping("/admin/product")
@RestController
public class FileUploadController {

    @Autowired
    MinioService minioService;

    @PostMapping("fileUpload")
    public Result<String> uploadFile(@RequestPart("file") MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String upload = minioService.upload(inputStream, null, filename);
            return Result.ok(upload);
        } catch (Exception e) {
            return Result.fail("网络异常");
        }
    }
}
